﻿using CKS.Schedule;
using Infrastructure;
using JinianNet.JNTemplate.Caching;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZR.Infrastructure;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.ServiceCore.Signalr;
using static Org.BouncyCastle.Asn1.Cmp.Challenge;

namespace CKS.PLC.Core.CronJob
{
    /// <summary>
    /// 01大屏推送示例
    /// </summary>
    [Schedule]
    public class Push01Job : CronJobBase
    {
        IHubContext<MessageHub> SignalHub;

        IFreeSql<MesDB> fsql;
        public Push01Job() : base("推送数据服务", "0/1 * * * * ?")  
        {
            fsql = InternalApp.Fsql;
        }


   

        protected override async Task ExecuteAsync(CancellationToken cancellationToken)
        {
            /*
              先组织所有的数据，再将数据一次推送给大屏客户端
              本示例数据均为展示需要。 实际项目可能涉及到查询数据库进行统计
              
             */
            SignalHub = InternalApp.ServiceProvider.GetService<IHubContext<MessageHub>>();
            if (SignalHub == null)
                return;

            /*
             假如有10个工站信息
             */
            Dictionary<int,string> namesdic = new Dictionary<int, string>() {
                { 1,"PCBAOP10"},
                { 2,"PCBAOP20"},
                { 3,"PCBAOP30"},
                { 4,"PCBAOP40"},
                { 5,"PCBAOP50"},
                { 6,"PCBAOP60"},
                { 7,"PCBAOP70"},
                { 8,"PCBAOP80"},
                { 9,"PCBAOP90"},
                { 10,"PCBAOP100"}
            };


            //报警信息




            var response = new Push01Response();
           
            
            response.Alarms = new List<DemoAlarms>();
            for (int i = 1; i <= 12; i++)
            {
                response.Alarms.Add(new DemoAlarms() { 
                    Id = i, Content = $"工位D{i} - 机械臂偏差", 
                    AlarmTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") });
            }


         



            double rate = 0.7 + new Random().NextDouble() * 0.3;
            response.OEE = new DemoOEE() {  FailureTime ="01:00:00", 
                OperatingRate = $"{rate:P2}",RunTime ="09:53:52",OkCount = 9900,NgCount=100,TotalCount=10000  };
            response.DemoStationData = new List<DemoStationData>();
            foreach (var id in namesdic.Keys)
            {
                DemoStationData data = new DemoStationData() { 
                    EnterTime = DateTime.Now.AddMinutes(-2).ToString("yyyy-MM-dd HH:mm:ss"),
                    ExitTime = DateTime.Now.AddMinutes(-1).ToString("yyyy-MM-dd HH:mm:ss"),
                    ProccessTime = "60秒",
                    SN = $"PCBA-DEMO-{ new Random().Next(1000,10000) }",
                    Name = namesdic[id]
                };
                response.DemoStationData.Add(data);
            }

            await SignalHub.Clients.Clients(MessageHub.Demo01Users)
                .SendAsync("bigScreenDemo01Receive", response);

            await Task.CompletedTask;
        }
    }
}
